<template>
    <span>&nbsp;&nbsp;{{nowTime}}&nbsp;距离{{targetDate}}正冬至还有{{time}}</span>
</template>
<script>
export default {
  name: 'TimeAndDate',
  data () {
    return {
      time: '',
      flag: false,
      targetDate: '2019年12月22日',
      nowTime: new Date().toLocaleDateString().split('/').join('-')
    }
  },
  mounted () {
    let time = setInterval(() => {
      if (this.flag === true) {
        clearInterval(time)
      }
      this.timeDown()
    }, 500)
  },
  props: {
    endTime: {
      type: String
    }
  },
  methods: {
    timeDown () {
      const endTime = new Date(this.endTime)
      const nowTime = new Date()
      let leftTime = parseInt((endTime.getTime() - nowTime.getTime()) / 1000)
      let d = parseInt(leftTime / (24 * 60 * 60))
      // let h = this.formate(parseInt(leftTime / (60 * 60) % 24))
      // let m = this.formate(parseInt(leftTime / 60 % 60))
      // let s = this.formate(parseInt(leftTime % 60))
      if (leftTime <= 0) {
        this.flag = true
        this.$emit('Time-end')
      }
      // 需要修改样式，在这里进行
      // this.time = `${d}天${h}小时${m}分${s}秒`
      this.time = ` ${d} 天`
    },
    formate (time) {
      if (time >= 10) {
        return time
      } else {
        return `0${time}`
      }
    }
  }
}
</script>
<style scoped>

</style>
